package AuthorityAgent;

import jade.content.ContentElement;
import jade.content.ContentManager;
import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import wwtp.ontology.Hour;
import wwtp.ontology.SendWater;
import DataBases.ToxicNotificationObject;

public class NotificationsReceiverBehaviour extends CyclicBehaviour{
	
	protected MessageTemplate mt;
	protected ContentManager manager;
	@Override
	public void action() {
		// TODO Auto-generated method stub
		mt = MessageTemplate.and(MessageTemplate.MatchConversationId("Notification"),MessageTemplate.MatchPerformative(ACLMessage.INFORM));
		manager = myAgent.getContentManager();
		final ACLMessage msg = myAgent.receive(mt);
        if (msg!=null){
    		try{
    		 	ContentElement ce = manager.extractContent(msg);
	   			if(ce instanceof SendWater){
	   				Hour hour = ((SendWater)ce).getHour();
	   				ToxicNotificationObject registro = new ToxicNotificationObject(hour.getHours(),hour.getMinutes(),hour.getSeconds(), ((SendWater)ce).getOrigin().getName());
	   				((AuthorityAgent)myAgent).dbToxicNotification.addItem(registro);
	    		}
	    		
    		}
   			catch (Exception e) {
   				e.printStackTrace();
   			}
        } else {
        	block();
        }
	}

}
